package com.ekang.admin.service;


import com.ekang.admin.dao.MemberFriendMapper;
import com.ekang.admin.dao.MemberMapper;
import com.ekang.admin.dao.UserMapper;
import com.ekang.admin.model.Member;
import com.ekang.admin.model.User;
import com.ekang.admin.utils.CommonUtils;
import com.ekang.easeim.api.impl.EasemobIMUsers;
import io.swagger.client.model.RegisterUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zcz on 2017/10/27.
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    MemberFriendMapper memberFriendMapper;


    public User getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }


    public Integer createUser(User user) {
        return userMapper.insertUser(user);
    }


    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }


    public User getUserById(Integer id) throws Exception {
        if (id != null) {
            //throw new Exception("nonodata");
        }
        return userMapper.getUserById(id);
    }


    public Integer deleteUserByIds(Integer... ids) {
        return userMapper.deleteUsers(ids);
    }


    public Integer deleteUserRole(Integer userId, Integer roleId) {
        return userMapper.deleteUserRole(userId, roleId);
    }


    public Integer addUserRole(Integer userId, Integer roleId) {
        return userMapper.addUserRole(userId, roleId);
    }


//    public PagingResp<Role> queryRoleByPaging(PagingReq req) {
//        List<Role> perms = userMapper.queryRoleByPaging(req);
//        Long count = userMapper.queryRoleCountByPaging(req);
//        PagingResp<Role> resp = new PagingResp<Role>();
//        resp.setData(perms);
//        resp.setDraw(req.getDraw());
//        resp.setRecordsTotal(count);
//        resp.setRecordsFiltered(count);
//        return resp;
//    }


    public Object getMemberById(Integer id) {

        return memberMapper.getMemberById(id);
    }


    public Object updateMember(Member member) {

        memberMapper.updatMember(member);
        return member;
    }


    public Object addMember(Member member) {
        member.setLastUpdateTime(System.currentTimeMillis());
        memberMapper.insertMember(member);
        Member memberData = memberMapper.getMemberById(member.getUid());
        return memberData;
    }


    public Object getWxMember(Member member) {
        String wxopenId = member.getWxOpenId();
        Member memberWx = memberMapper.getMemberByWxOpenID(wxopenId);
        if (memberWx != null) {
            return memberWx;
        }

        Long timestamp = System.currentTimeMillis();
        member.setLastUpdateTime(timestamp);
        member.setWxbindtime(timestamp);
        member.setStatus(1);
        member.setUnionid(CommonUtils.uuid());//easeIM userID
        memberMapper.insertMember(member);
        Member memberData = memberMapper.getMemberById(member.getUid());

        EasemobIMUsers easemobIMUsers = new EasemobIMUsers();

        RegisterUsers users = new RegisterUsers();
        io.swagger.client.model.User user = new io.swagger.client.model.User().username(member.getUnionid()).password(member.getUnionid());
        users.add(user);
        easemobIMUsers.createNewIMUserSingle(users);
        return memberData;
    }

    public Object getMemberListByUserId(Integer uid) {
        return memberFriendMapper.getMemberListByUserId(uid);
    }
}
